//
//  HCFindNaviView.m
//  Aierle
//
//  Created by jiayibai-02por on 2017/5/24.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCFindNaviView.h"

@interface HCFindNaviView()
{
    CGFloat minWidth_;
    UIButton *rightButton_;
    UIScrollView *naviScroll_;
    NSArray *colorArray_;
    NSInteger index_;
}

@end

@implementation HCFindNaviView

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        
        [self initializeView];
    }
    return self;
}

- (void)initializeView {

    self.backgroundColor = YBKRGBCOLOR(241, 241, 241, 1);
    minWidth_ = ([UIScreen mainScreen].bounds.size.width-20)/4.0;
    naviScroll_ = [[UIScrollView alloc] initWithFrame:self.bounds];
    naviScroll_.scrollEnabled = YES;
    naviScroll_.backgroundColor = YBKRGBCOLOR(241, 241, 241, 1);
    naviScroll_.contentSize = CGSizeMake(Screen_width, 0);
    naviScroll_.showsVerticalScrollIndicator = NO;
    naviScroll_.showsHorizontalScrollIndicator = NO;
    [self addSubview:naviScroll_];
    
    UIColor *color1 = YBKRGBCOLOR(79, 211, 190, 1);
    UIColor *color2 = YBKRGBCOLOR(253, 152, 59, 1);
    UIColor *color3 = YBKRGBCOLOR(40, 182, 252, 1);
    UIColor *color4 = YBKRGBCOLOR(237, 97, 99, 1);
    colorArray_ = @[color1,color2,color3,color4];
    index_ = 0;
}

//配置数据
- (void)setNamesArray:(NSArray *)namesArray {

    _namesArray = namesArray;
    if (namesArray.count != 0) {
        
        //加载之前先移除所有button，防止重复
        for (UIView *subview in self.subviews) {
            
            if ([subview isKindOfClass:[UIButton class]]) {
                
                [subview removeFromSuperview];
            }
        }
        for (int i = 0; i < namesArray.count; i++) {
            
            UIFont *font = [UIFont systemFontOfSize:14];
            NSDictionary *attribute = @{NSFontAttributeName:font};
            CGSize size = [namesArray[i] sizeWithAttributes:attribute];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.tag = 10 + i;
            if (size.width+15 <= minWidth_) {
                
                [button setFrame:CGRectMake(4 + CGRectGetMaxX(rightButton_.frame), 4, minWidth_, YBKAdaptiveH(50))];
            }
            else {
                
                [button setFrame:CGRectMake(4 + CGRectGetMaxX(rightButton_.frame), 4, size.width + 15, YBKAdaptiveH(50))];
            }
            [button setTitle:namesArray[i] forState:UIControlStateNormal];
            button.titleLabel.font = font;
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            if (index_ >= colorArray_.count) {
                index_ = 0;
            }
            button.backgroundColor = colorArray_[index_];
            [button addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
            [naviScroll_ addSubview:button];
            rightButton_ = button;
            if (CGRectGetMaxX(rightButton_.frame) + 4 > Screen_width) {
                
                naviScroll_.contentSize = CGSizeMake(CGRectGetMaxX(rightButton_.frame) + 4, 0);
            }
            index_++;
        }
    }
}

//buton点击事件
- (void)buttonClickAction:(UIButton *)sender {

    if ([self.delegate respondsToSelector:@selector(clickButtonAction:)]) {
        
        [self.delegate clickButtonAction:sender.tag-10];
    }
}

@end
